package com.liuning.websocketdemo.controller;

import com.liuning.websocketdemo.entity.User;
import com.liuning.websocketdemo.pojo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(String username,String password, HttpSession session) {
        System.out.println(username + "===" + password);
        Result result = new Result();
        if (username != null && password.equals("111")) {

            result.setFlag(true);
            //将用户名存储到session中
            session.setAttribute("user",username);
        }else {
            result.setFlag(false);
            result.setMessage("密码错误");
        }
        return result;
    }

    @GetMapping("/main")
    public String toMainPage() {
        return "main";
    }

    @GetMapping("/getUsername")
    @ResponseBody
    public String getUsername(HttpSession session) {
        String username = (String) session.getAttribute("user");
        return username;
    }

}
